package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.millioncall.openplatform.common.PageNoSizePairImpl;
import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.ProductDAO;
import org.millioncall.yueyoga.admin.model.AviatorExpression;
import org.millioncall.yueyoga.admin.model.ConsumeHistoryModel;
import org.millioncall.yueyoga.admin.model.Product;
import org.millioncall.yueyoga.admin.service.advancedsearch.AviatorResultHandler;

public class ProductDAOImpl extends BaseDao implements ProductDAO {

	@Override
	public Product findById(Map<String, Object> map) {
		Product product = null;
		product = this.getPagableSqlSessionTemplate().selectOne(
				"findProductById", map);
		return product;
	}

	@Override
	public PaginatedList<Product> findInPage(Map<String, Object> map,
			int pageNo, int pageSize) {
		PreparedQuery<Product> pq = this.getPagableSqlSessionTemplate()
				.findAll("findProductsByConditions", map);
		PaginatedList<Product> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(pageNo, pageSize)));
		return pl;
	}

	@Override
	public PaginatedList<Product> findInPage(int start, int limit) {
		PreparedQuery<Product> pq = this.getPagableSqlSessionTemplate()
				.findAll("findAllProduct");
		PaginatedList<Product> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public int save(Product product) {
		int i = 0;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", product.getName());
			map.put("type", product.getType());
			map.put("counts", product.getCounts());
			map.put("beginTime", product.getBeginTime());
			map.put("endTime", product.getEndTime());
			map.put("price", product.getPrice());
			map.put("limitOfWeek", product.getLimitOfWeek());
			map.put("remarks", product.getRemarks());
			map.put("enable", product.getEnable());
			map.put("organizationId", product.getOrganizationId());
			i = this.getPagableSqlSessionTemplate().insert("saveProduct", map);
		} catch (Exception e) {
			throw e;
		}
		return i;
	}

	@Override
	public int update(Product product) {
		int i = 0;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("productId", product.getProductId());
			map.put("name", product.getName());
			map.put("type", product.getType());
			map.put("counts", product.getCounts());
			map.put("beginTime", product.getBeginTime());
			map.put("endTime", product.getEndTime());
			map.put("price", product.getPrice());
			map.put("limitOfWeek", product.getLimitOfWeek());
			map.put("remarks", product.getRemarks());
			map.put("enable", product.getEnable());
			i = this.getPagableSqlSessionTemplate()
					.update("updateProduct", map);
		} catch (Exception e) {
			throw e;
		}
		return i;
	}

	@Override
	public int deleteByIds(String[] ids) {
		int Id = 0;
		for (String strId : ids) {
			try {
				Id = Integer.parseInt(strId);
				deleteById(Id);
			} catch (NumberFormatException e) {
				return 0;
			}
		}
		return 1;
	}

	public void deleteById(Integer Id) {
		try {
			this.getPagableSqlSessionTemplate().delete("deleteProduct", Id);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public PaginatedList<Product> findAll() {
		try {
			PreparedQuery<Product> pq = this.getPagableSqlSessionTemplate()
					.findAll("findAllProduct");
			PaginatedList<Product> pl = pq.list(null);
			return pl;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public int delete(Product product) {
		int i = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("productId", product.getProductId());
		i = this.getPagableSqlSessionTemplate().delete("delete", map);
		return i;
	}

	@Override
	public PaginatedList<Product> findProductAvailable(int start, int limit,
			Map<String, Object> map) {
		PreparedQuery<Product> pq = this.getPagableSqlSessionTemplate()
				.findAll("findProductAvailable", map);
		PaginatedList<Product> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public List<Product> findProductAvailableAll(Map<String, Object> map) {
		PreparedQuery<Product> pq = this.getPagableSqlSessionTemplate()
				.findAll("findAllProduct", map);
		List<Product> pl = pq.list();
		return pl;
	}

	@Override
	public PaginatedList<Product> findByMemberIdInPage(Integer memberId,
			int start, int limit) {
		PreparedQuery<Product> pq = this.getPagableSqlSessionTemplate()
				.findAll("findProductByMemberIdInPage", memberId);
		PaginatedList<Product> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public PaginatedList<ConsumeHistoryModel> findAllConsumeHistory(
			Map<String, Object> params, Integer pageNo, Integer pageSize) {
		PreparedQuery<ConsumeHistoryModel> pq = this
				.getPagableSqlSessionTemplate().findAll(
						"findAllConsumeHistory", params);
		PaginatedList<ConsumeHistoryModel> pl = pq.list(new PaginateParams(
				new PageNoSizePairImpl(pageNo, pageSize)));
		return pl;
	}

	@Override
	public PaginatedList<ConsumeHistoryModel> findAllAbsentForSomeDays(
			Map<String, Object> params, Integer pageNo, Integer pageSize) {
		PreparedQuery<ConsumeHistoryModel> pq = this
				.getPagableSqlSessionTemplate().findAll(
						"findAllAbsentForSomeDays", params);
		PaginatedList<ConsumeHistoryModel> pl = pq.list(new PaginateParams(
				new PageNoSizePairImpl(pageNo, pageSize)));
		return pl;
	}

	@Override
	public PaginatedList<Product> findProductAvailable(Map<String, Object> map,
			int start, int pageSize) {
		PreparedQuery<Product> pq = this.getPagableSqlSessionTemplate()
				.findAll("findProductAvailables", map);
		PaginatedList<Product> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, pageSize)));
		return pl;
	}

	@Override
	public PaginatedList<Product> findMemberInfoInPage(Map<String, Object> map,
			Integer start, Integer limit) {
		PreparedQuery<Product> pq = this.getPagableSqlSessionTemplate()
				.findAll("findMemberInfoInPage", map);
		PaginatedList<Product> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public List<Product> findByMemberId(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectList("findByMemberId",
				map);
	}

	@Override
	public List<Product> getAllProductsWithAviator(Map<String, Object> params,
			AviatorExpression expression, Map<String, Object> aviatorParams,
			int limit) {
		AviatorResultHandler<Product> handler = new AviatorResultHandler<Product>(
				expression, aviatorParams, limit);
		this.getPagableSqlSessionTemplate().select("findProductsByConditions",
				params, new RowBounds(0, Integer.MAX_VALUE), handler);
		return handler.getResultList();
	}

	@Override
	public void clearWeeklyUseTimes() {
		this.getPagableSqlSessionTemplate().update("clearWeeklyUseTimes");
	}

}
